package pack;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import dao.ChestionareDAORemote;
import dao.IntrebariDAORemote;
import dao.RaspunsuriCompletateDAORemote;
import dao.RaspunsuriDAORemote;
import dto.ChestionareDTO;
import dto.EmployeeDTO;
import dto.RaspunsuriCompletateDTO;

@ManagedBean(name = "hrBean")
@SessionScoped
public class HrChestionareBean implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@EJB
	ChestionareDAORemote chestionareDAO;
	
	@EJB
	IntrebariDAORemote intrebariDAO;
	
	@EJB
	RaspunsuriCompletateDAORemote raspunsuriCompletateDAO;

	@EJB
	RaspunsuriDAORemote raspunsuriDAO;
	
	List<ChestionareDTO> chestionare;
	
	List<RaspunsuriCompletateDTO> rc;
	
	private int selected;
	
	private String fisier;
	
	public void verify(){
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");

		ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc
				.getApplication().getNavigationHandler();

		if (eDTO != null) {
			if (eDTO.getRank() != 2) {
				nav.performNavigation("home.xhtml");
			}
		} else {
			nav.performNavigation("home.xhtml");
		}
		setChestionare(chestionareDAO.findAllChestionare());
	}
	
	public String generateRaspunsuri() throws IOException{
		List<ChestionareDTO> cList = new ArrayList<ChestionareDTO>();
		cList = chestionareDAO.findAllChestionare();
		ChestionareDTO c = new ChestionareDTO();
		
		String ss = "D:\\LICENTA\\Fisiere_exportate\\" + getFisier() + ".csv";
		FileWriter writer = new FileWriter(ss);
		
		writer.append("Intrebare" + ","
				+ "Raspuns" + "," 
				+ "Angajat" + "\n");
		
		for (int i = 0; i < cList.size(); i++)
			if (cList.get(i).getId() == getSelected())
				c = cList.get(i);
		for (int i = 0; i < c.getIntrebari().size(); i++){
			List<RaspunsuriCompletateDTO> rcByIntId = new ArrayList<RaspunsuriCompletateDTO>();
			rcByIntId = raspunsuriCompletateDAO.getRCByIntrebareId(c.getIntrebari().get(i).getId());
			for (int j = 0; j < rcByIntId.size(); j++){
				writer.write(c.getIntrebari().get(i).getIntrebare() +
						"," + rcByIntId.get(j).getRaspuns() +
						"," + rcByIntId.get(j).getAngajatName() + "\n");
			}
		}
		writer.flush();
		writer.close();
		return "";
	}

	public List<ChestionareDTO> getChestionare() {
		return chestionare;
	}

	public void setChestionare(List<ChestionareDTO> chestionare) {
		this.chestionare = chestionare;
	}

	public List<RaspunsuriCompletateDTO> getRc() {
		return rc;
	}

	public void setRc(List<RaspunsuriCompletateDTO> rc) {
		this.rc = rc;
	}

	public int getSelected() {
		return selected;
	}

	public void setSelected(int selected) {
		this.selected = selected;
	}

	public String getFisier() {
		return fisier;
	}

	public void setFisier(String fisier) {
		this.fisier = fisier;
	}

}
